// pages/userDetail/userDetail.js
const {
  getUserById,getPostByUserId,isLikePost
} = require('../../apis/api')

import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msg1:'1',  
    noramalData: [],
    UserDataList:[],
    active: 0,
    loadingMore: false,
    huozan:"0",
    guanzhu:"0",
    fan:"0",
    show2:false,
    deleid:"",
    userid:"",
    name:"",
    mail:"",
    pic:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userid:options.id
    })
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['username', 'userAvatar', 'email', 'token'],
      actions: ['setUsername', 'setUserAvatar', 'setEmail', 'setToken'],
    })
  },
  goDetail(e){
    wx.navigateTo({
      url: '/pages/detail/detail?nid='+e.currentTarget.dataset.nid,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    getUserById(this.data.token,this.data.userid,(res)=>{
      if(res.data.code===200){
        console.log(res.data.data)
        this.setData({
          name:res.data.data.userName,
          mail:res.data.data.emailAccount,
          pic:res.data.data.pictureUrl,
          huozan:res.data.data.likes,
          guanzhu:res.data.data.followedUsers,
          fan:res.data.data.fans
        })
      }
    })
    getPostByUserId(this.data.token,this.data.userid,(res)=>{
      if(res.data.code===200){
        this.setData({
          noramalData:res.data.data
        })
        var that = this
        const arr = this.data.noramalData.map((item) => {
          return {
            ...item,
            tx: that.data.pic,
            name: that.data.name
          };
        });
        this.setData({
          noramalData: arr
        });
        // 在此处进行点赞信息的处理
        const likePromises = this.data.noramalData.map((item) => {
          return new Promise((resolve) => {
            isLikePost(this.data.token, item.post_id, (res) => {
              if (res.data.code === 200) {
                resolve({
                  ...item,
                  love: res.data.data==='true'?true:false
                });
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                });
                resolve(item); // Return the original item in case of error.
              }
            });
          });
        });
    
        Promise.all(likePromises).then((updatedDataWithLikes) => {
          this.setData({
            noramalData: updatedDataWithLikes,
          });
          // 数据更新后再输出
          console.log(this.data.noramalData);
        });
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})